﻿package PinballCode {
	
	import flash.display.Shape;
	
	public class Spinner extends Shape {

		//Attributes
		private var radius1:Number;                 //the inside, smaller radius that the vertices move back to to make fins
		private var radius2:Number;					//the outside, larger radius for the long parts of the spinner
		private var color1:uint;                     //Border color
		private var color2:uint;                          //fill color
		private var vertices:Array;                        //holds all the vertices to be plotted
		
		private var startVertex:Vertex;            //used for looping through the vertices and drawing all of them
		private var currentVertex:Vertex;
		public var walls:Array;
		
		private var center:Vertex;                    //center of the shape
		public var borderThickness:Number;                    //how thick the border will be
		var theta:Number;                               //the angle at which to add the next vertex
		var rads:Number;								//the angle at which to add the next vertex in radians
		
		//constructor
		public function Spinner(center:Vertex, radius1:Number=10, radius2:Number = 20, borderThickness = 2, color1:uint =0x000000, color2:uint=0x00FF00) {
			this.center = center;
			this.radius1 = radius1;
			this.radius2 = radius2;
			this.borderThickness = borderThickness;
			this.color1 = color1;
			this.color2 = color2;
			vertices = new Array();
			walls = new Array();
			init();
		}
		
		public function init():void {
			makeVertices();
			renderVertices();
		}
		
		//populates the vertices array
		// adds vertices like the polygon did, but changes the amount of rotation each iteration
		public function makeVertices():void {
	
			theta = 0;
			
			//a spinner has 8 sides, so for each side it adds a vertex.  If the side is even, it also adds a vertex on the smaller 
			//radius so that the shape looks like a spinner.
			for(var x:Number = 1; x<= 8; x++)
			{
				if (x%2 ==0)       //if the iteration number is even, add a vertex in the small radius and then
				{						//move a large distance to add a vertex
					vertices.push(new Vertex(center.x+radius1*Math.cos(rads),center.y+radius1*Math.sin(rads)));
					//walls.push(new Wall(vertices[x - 1],vertices[x]))
					theta +=70;
				}
				else                      //if it's odd, move a large distance. this will make the x shape
				{
					theta += 20;
				}
				
				rads = theta * Math.PI / 180;      //switch to radians
				vertices.push(new Vertex(center.x+radius2*Math.cos(rads),center.y+radius2*Math.sin(rads)));			
				if(x > 1)
				walls.push(new Wall(vertices[x - 1],vertices[x]));
			}
			walls.push(new Wall(vertices[vertices.length - 1],vertices[0]));
			
		}
		
		public function renderVertices():void {
			
			graphics.beginFill(color2, alpha=1.0);
			
			graphics.lineStyle(borderThickness, color1, alpha=1.0);
			
			startVertex = vertices[0];
			graphics.moveTo(startVertex.x, startVertex.y);
			
			for (var x:Number = 1; x < vertices.length; x++)
			{
				currentVertex = vertices[x];                        //set the current vertex to the next on the array
				graphics.lineTo(currentVertex.x, currentVertex.y);  //draw the line to that vertex
			}
			
			graphics.lineTo(startVertex.x, startVertex.y);      //draw line to complete the shape
			
			graphics.endFill();
		}
	}
	
	
}
